package com.javapda.app.camera.gui;

import java.awt.Color;

import javax.swing.JButton;

import com.javapda.util.MiscUtil;

public class PreviewRunnable implements Runnable {
	private boolean running = false;
	private CameraFrame cameraFrame;
	private static int count;
	private static long startTime;
	public PreviewRunnable(CameraFrame cf) {
		this.cameraFrame = cf;
		
	}
	public boolean isRunning() { return running; }
	public void setRunning(boolean r) { running = r; }
	public void run() {
		running = true;
		startTime = System.currentTimeMillis();
		while (running) {
			cameraFrame.getPreviewButton().setEnabled(false);
			((JButton)cameraFrame.getPreviewButton()).setText("G-Preview");
			((JButton)cameraFrame.getPreviewButton()).setForeground(Color.GREEN);
			MiscUtil.pause(500);
			cameraFrame.getPreviewButton().setEnabled(true); 
			((JButton)cameraFrame.getPreviewButton()).setText("N-Preview");
			((JButton)cameraFrame.getPreviewButton()).setForeground(Color.GRAY);
			if (!cameraFrame.preview()) {
				//cameraFrame.syncCamera();
			} else {
				count++;
				double duration = (System.currentTimeMillis()-startTime)/1000.;
				String sep="|";
				StringBuilder sb = new StringBuilder();
				sb.append("No. Previews: "+count);
				sb.append(sep);
				sb.append("duration: " + duration + " sec");
				System.out.println(sb.toString());
			}
			
		}
		System.out.println("STOP PREVIEW THREAD");
	}

}
